﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;

namespace School.Organization
{
    public static class Structure
    {
        private static SchoolEntities db = new SchoolEntities();

        public static IQueryable<ORG_Campus> GetAllCampuses()
        {
            var result = from c in db.ORG_Campus
                         orderby c.Name
                         select c;

            return result;
        }

        public static void AddCampus(ORG_Campus campus)
        {
            long id = (from c in db.ORG_Campus select c.Id).Max() + 1;
            campus.Id = id;
            db.AddToORG_Campus(campus);
            db.SaveChanges();
        }

        public static ORG_Campus GetCampus(long id)
        {
            return db.ORG_Campus.Single(x => x.Id == id);
        }

        public static void UpdateCampus(ORG_Campus campus)
        {
            ORG_Campus model = (from c in db.ORG_Campus
                    where c.Id == campus.Id
                    select c).First();

            model.Name = campus.Name;
            model.Description = campus.Description;

            db.SaveChanges();
        }

        public static IEnumerable<ORG_Certificate> GetAllCertificates()
        {
            var certificates = from c in db.ORG_Certificate
                    orderby c.Name
                    select c;

            return certificates;
        }

        public static void AddCertificate(ORG_Certificate certificate)
        {
            var result = from x in db.ORG_Certificate select x.Id;
            long id = 0;

            if (result.Count() == 0)
                id = 1;

            else
                id = (from x in db.ORG_Certificate select x.Id).Max() + 1;

            db.AddToORG_Certificate(new ORG_Certificate() { Id = id, Name = certificate.Name, Description = certificate.Description, Campus_Id = certificate.Campus_Id });
            db.SaveChanges();
        }

        public static ORG_Certificate GetCertificate(long id)
        {
            var certificate = db.ORG_Certificate.Single(x => x.Id == id);

            return certificate;
        }

        public static void UpdateCertificate(ORG_Certificate certificate)
        {
            ORG_Certificate c = (from x in db.ORG_Certificate where x.Id == certificate.Id select x).First();
            c.Name = certificate.Name;
            c.Description = certificate.Description;
            c.Campus_Id = certificate.Campus_Id;            
            db.SaveChanges();            
        }
    }
}
